package com.uacp.sgb.vista.controladores;

import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import com.uacp.sgb.datos.dominio.entidades.Usuario;
import com.uacp.sgb.datos.dominio.persistencia.dao.UsuarioDao;

@ManagedBean(name="loginBean")
@SessionScoped
public class ControlLogin 
{
    private String username;  
    private String password;  
    private Usuario usuario;
    private boolean loggedIn = false;
    
    public String loginUsuario()
	{	
		usuario = UsuarioDao.validar(username, password);
		
		if (usuario != null) 
		{
			loggedIn=true;
			return "exportacion.xhtml";
		} 
		else 
		{
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
			FacesContext.getCurrentInstance().addMessage(null, msg);
			username = "";
			password = "";
			return null;
		}
	}
    
	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public String getUsername() {  
        return username;  
    }  
  
    public void setUsername(String username) {  
        this.username = username;  
    }  
  
    public String getPassword() {  
        return password;  
    }  
  
    public void setPassword(String password) {  
        this.password = password;  
    } 
    
    public void checkLogin(ComponentSystemEvent event){
   
		if(!loggedIn){
    		FacesContext context = FacesContext.getCurrentInstance();
    		ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler)
    		context.getApplication().getNavigationHandler();
    		handler.performNavigation("login");
    	}
    }
    
    public void cerrarSesion(){
    	System.out.println("david");
    	loggedIn=false;

    
    }	
}
